package com.beiding.jsonanalyzer.querylink;

import com.beiding.jsonanalyzer.flow.ValueNode;
import com.beiding.jsonanalyzer.flow.BranchGateway;
import com.beiding.jsonanalyzer.flow.BranchNode;
import com.beiding.jsonanalyzer.flow.ComputeResource;
import com.beiding.jsonanalyzer.flow.flowevents.DataNodeEvent;
import com.beiding.jsonanalyzer.visit.VisitAble;

public class UseComputeResourceQueryLinkNode<W extends ValueNode> extends QueryLinkNodeAdapter<W> {

    private ComputeResource computeResource;

    public UseComputeResourceQueryLinkNode(QueryLinkNode<W> pre, ComputeResource computeResource) {
        super(pre);
        this.computeResource = computeResource;
    }

    @Override
    protected VisitAble<BranchNode<W>> doExec(VisitAble<BranchNode<W>> preResult) {
        return visitor -> {
            preResult.visit(node -> {

                //在此创建一个gateway

                BranchGateway<W> branchGateway = new BranchGateway<>(node.getBranch(), computeResource, node.getBranch().getBranchReferLink(),node.getBranch().getValueNodeFactory());
                String n = "<compute-resource:" + computeResource.name() + ">";
                BranchGateway<W>.BranchResult oneBranch = branchGateway.getOneBranch("compute-resource", n);
                BranchNode<W> branchNode = oneBranch.getBranchNode();
                visitor.onNode(branchNode);

                //绑定数据订阅
                ComputeNodeUtils.bindControlEvent(node, branchNode);
                node.onDataEvent(DataNodeEvent.eventType(), branchNode::signDataEvent);


            });
        };
    }
}
